from allpairspy import AllPairs

from api_py.enums.cookie import Cookie
from api_py.generate_case.template import template_case
from api_py.utils.file_sys import write_json

username = [
    {"value": "", "description": "用户名为空"},
    {"value": "username", "description": "合法用户名"},
    {"value": "aaa", "description": "用户名长度小于4"},
    {"value": "aaaaaaaaaaaaaaaaaaaaaaaaaa", "description": "用户名长度长度大于24"},
]

password = [
    {"value": "", "description": "密码为空"},
    {"value": "aaaaaa", "description": "密码合法"},
    {"value": "aaa", "description": "密码长度小于6"},
    {"value": "aaaaaaaaaaaaaaaaaaaaaaaaaa", "description": "密码长度长度大于20"},
]

role = [
    {"value": "ROLE_SUPERVISOR", "description": "角色为督导"},
    {"value": "ROLE_COUNSELOR", "description": "角色为咨询师"},
    {"value": "OTHER", "description": "非法角色"},
]

cookies = [
    {"value": Cookie.ADMIN_COOKIE.value, "description": "管理员cookie"},
    {"value": Cookie.SUPERVISOR_COOKIE.value, "description": "督导cookie"},
    {"value": Cookie.COUNSELOR_COOKIE.value, "description": "咨询师cookie"},
    {"value": Cookie.WEIXIN_COOKIE.value, "description": "微信访客cookie"},
    {"value": Cookie.NULL_COOKIE.value, "description": "无cookie"},
    {"value": Cookie.ERROR_COOKIE.value, "description": "其他cookie"},
]

# 检测权限用例
body = [
    {"value": "username", "description": "合法用户名"},
    {"value": "aaaaaa", "description": "密码合法"},
    {"value": "ROLE_SUPERVISOR", "description": "角色为督导"},
]


def case05_user_ms(api_type):
    json_result = {}
    parameters = [username, password, role]
    i = 0

    # 检测数据格式用例
    for pairs in AllPairs(parameters):
        case = template_case()
        request = case["request"]
        wrap_data(request, pairs)
        # 包装头部信息
        request["headers"]["Cookie"] = cookies[0]

        json_result[i] = case
        i += 1

    for cookie in cookies:
        case = template_case()
        request = case["request"]
        wrap_data(request, body)
        # 包装头部信息
        request["headers"]["Cookie"] = cookie

        json_result[i] = case
        i += 1


    write_json(api_type.value[0]["file_name"], json_result)


def wrap_data(request, data):
    request["body"]["username"] = data[0]
    request["body"]["password"] = data[1]
    request["body"]["role"] = data[2]